package com.example.script.admin.service.impl;

import com.example.script.admin.enums.ScriptResult;
import com.example.script.admin.model.dto.ScriptPublishDto;
import com.example.script.admin.service.ProductService;
import com.example.script.admin.service.ScriptService;
import com.example.script.mapper.ScriptNameMapper;
import com.example.script.mapper.ScriptTypeMapper;
import com.example.script.model.ScriptName;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ScriptServiceImpl implements ScriptService {
    @Autowired
    private ScriptTypeMapper scriptTypeMapper;
    @Autowired
    private ScriptNameMapper scriptNameMapper;
    @Autowired
    private ProductService productService;

    int result = 0;

    /**
     * 添加剧本
     *
     * @param scriptPublishDto 添加剧本的信息
     * @return
     */
    @Override
    public ScriptResult addScript(ScriptPublishDto scriptPublishDto) {
        List<ScriptName> sameScriptNameList = productService.findScriptByName(scriptPublishDto.getScriptName());
        ScriptName scriptName = new ScriptName();
        if (CollectionUtils.isNotEmpty(sameScriptNameList) &&
                !Objects.equals(sameScriptNameList.get(0).getId(), scriptPublishDto.getId())) {
            scriptName.setScriptname(scriptPublishDto.getScriptName());
            scriptName.setScriptnop(scriptPublishDto.getScriptNOP());
            scriptName.setScriptDuration(scriptPublishDto.getScript_duration());
            scriptName.setScriptShortIntroduction(scriptPublishDto.getScriptShortIntroduction());
            scriptName.setScriptDetailedIntroduction(scriptPublishDto.getScriptDetailedIntroduction());
            scriptName.setCreatetionTime(new Date());
            scriptNameMapper.insertSelective(scriptName);
            return ScriptResult.UPDATE_SUCCESS;
        }
        return ScriptResult.SCRIPT_FAILED;
    }

    /**
     * 更新剧本
     *
     * @param scriptPublishDto
     * @return
     */
    @Override
    public int updateScript(ScriptPublishDto scriptPublishDto) {
        ScriptName scriptName = new ScriptName();
        List<ScriptName> sameScriptNameList = productService.findScriptByName(scriptPublishDto.getScriptName());
        if (CollectionUtils.isNotEmpty(sameScriptNameList) &&
                !Objects.equals(sameScriptNameList.get(0).getId(), scriptPublishDto.getId())) {
            scriptName.setScriptname(scriptPublishDto.getScriptName());
            scriptName.setScriptnop(scriptPublishDto.getScriptNOP());
            scriptName.setScriptDuration(scriptPublishDto.getScript_duration());
            scriptName.setScriptShortIntroduction(scriptPublishDto.getScriptShortIntroduction());
            scriptName.setScriptDetailedIntroduction(scriptPublishDto.getScriptDetailedIntroduction());
            scriptName.setModifyTime(new Date());
        }
        result = scriptNameMapper.updateByPrimaryKey(scriptName);
        if (result <= 0) {
            return 0;
        }
        return result;
    }

    /**
     * 删除剧本
     *
     * @param scriptPublishDto
     * @return
     */
    @Override
    public int deleteScript(ScriptPublishDto scriptPublishDto) {
        ScriptName scriptName = new ScriptName();
        scriptName.setId(scriptPublishDto.getId());
        scriptName.setScriptname(scriptPublishDto.getScriptName());
        scriptName.setScriptnop(scriptPublishDto.getScriptNOP());
        scriptName.setScriptDuration(scriptPublishDto.getScript_duration());
        scriptName.setScriptDetailedIntroduction(scriptPublishDto.getScriptDetailedIntroduction());
        scriptName.setScriptShortIntroduction(scriptPublishDto.getScriptShortIntroduction());
        result = scriptNameMapper.deleteByPrimaryKey(scriptName.getId());
        if (result <= 0) {
            return 0;
        }
        return result;
    }
}
